﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCS.ComLib.Config;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;
using System.Web.Security;
using VietThuongLMS.Areas.Admin.Models;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace VietThuongLMS.Areas.Admin.Controllers
{

    public class GhiChuController : Controller
    {
        //
        // GET: /Admin/GhiChu/
        [Authorize(Roles = "NhanVien")]
        [HttpGet, ValidateInput(false)]
        public ActionResult Index()
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            //string username = System.Web.HttpContext.Current.User.Identity.Name;
            //int account_id = AccountService.GetAccountIdByName(username);

            //int maNhanVien = NhanVienService.GetStaffIdByAccountID(account_id);

            GhiChuViewModel GhiChuList = GhiChuService.GetGhiChuList(int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "GhiChu", "Index", "?p=", GhiChuList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(GhiChuList);
        }


        protected string StripHtml(string Txt)
        {
            return Regex.Replace(Txt, "<(.|\\n)*?>", string.Empty);
        }

        //
        // GET: /Admin/GhiChu/Details/5
        [Authorize(Roles = "NhanVien")]
        public ActionResult Detail(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            GhiChuMoRongModel viewModel = new GhiChuMoRongModel();

            viewModel.gc = context.GhiChus.Join(context.NhanViens,
                gc => gc.MaNhanVien,
                nv => nv.ID,
                (gc, nv) => gc).Where(gc => gc.ID == id).FirstOrDefault();

            //viewodel.gc.NoiDung = StripHtml(viewModel.gc.NoiDung);
            return View(viewModel);
        }

        
        ////
        //// GET: /Admin/GhiChu/Edit/5
        [Authorize(Roles = "NhanVien")]
        public ActionResult Edit(int id)
        {
            GhiChu gc = GhiChuService.GetGhiChuById(id);
            return View(gc);
        }


        //// POST: /Admin/GhiChu/Edit/5
        [Authorize(Roles = "NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(GhiChu gc)
        {
            try
            {
                // TODO: Add update logic here
                bool result = GhiChuService.Edit(gc);

                if (result)
                    return RedirectToAction("Detail", "GhiChu", new { area = "Admin", @id = gc.ID });
                else
                    return View(gc);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("GhiChuController::Edit", ex.StackTrace, ex.Message);
                return RedirectToAction("Index", "GhiChu", new { area = "Admin" });
            }
        }

        //
        // GET: /Admin/NhanVien/Create
        [Authorize(Roles = "NhanVien")]
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Admin/NhanVien/Create
        [Authorize(Roles = "NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(GhiChu model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.Deleted = Utils.Constant.INACTIVE;

                    string username = System.Web.HttpContext.Current.User.Identity.Name;
                    int account_id = AccountService.GetAccountIdByName(username);
                    model.MaNhanVien = NhanVienService.GetStaffIdByAccountID(account_id);

                    bool result = GhiChuService.InsertGhiChu(model);
                    if (result)
                    {
                        return RedirectToAction("Index", "GhiChu", new { area = "Admin"});
                    }
                    return View("Create", model);
                }
                catch (Exception ex)
                {
                    DCS.ComLib.File.Log.Write("GhiChuController::Create", ex.StackTrace, ex.Message);
                    return View("Create", model);
                }
            }
            //Invalid, redisplay form with errors
            return RedirectToAction("Create", "GhiChu", new { area = "Admin"});
        }


        // POST: /Admin/GhiChu/Delete/5
        [Authorize(Roles = "NhanVien")]
        [HttpGet]
        public ActionResult Delete(int id, int maNhanVien)
        {
            // TODO: Add delete logic here

            bool result = GhiChuService.DeleteGhiChu(id);
            if (result)
            {
                return RedirectToAction("Index", "GhiChu", new { area = "Admin"});
            }
            else
            {
                return View();
            }
        }

    }
}
